package com.zt.bus.order.controller;

import com.zt.bus.order.model.OrderPayment;
import com.zt.bus.order.service.OrderPaymentService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import com.zt.util.DateUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/order/pay")
public class OrderPaymentController {

    @Autowired
    private OrderPaymentService orderPaymentService;

    /**
     * 步骤三：提交给支付平台前，生成支付信息
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/getPayment")
    @ResponseBody
    public JSONObject getOrderPayment(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        OrderPayment payment = orderPaymentService.getOrderPayment(reqBean.getInfo("payPlatform"),
                reqBean.getInfo("orderId"), reqBean.getInfo("bookUserPhone"));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("tradeNo", payment.getTradeNo());
        jsonObject.put("orderId", payment.getOrderId());
        jsonObject.put("bookUserPhone", payment.getBookUserPhone());
        jsonObject.put("cashAmount", payment.getCashAmount());
        jsonObject.put("payType", payment.getPayType());
        jsonObject.put("payPlatform", payment.getPayPlatform());
        jsonObject.put("orderTime", DateUtil.formatDateTime(payment.getOrderTime()));
        return Response.responseSuccess(jsonObject);
    }

    /**
     * 步骤四：与支付平台下单之后创建支付记录
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/createPayment")
    @ResponseBody
    public JSONObject createOrderPayment(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        OrderPayment payment = new OrderPayment();
        payment.setTradeNo(reqBean.getInfo("tradeNo")); // 交易流水号
        payment.setOrderId(reqBean.getInfo("orderId")); // 订单ID
        payment.setPayPlatformNo(reqBean.getInfo("payPlatformNo")); // 支付平台订单号，支付平台返回的流水号
        payment.setBookUserPhone(reqBean.getInfo("bookUserPhone")); // 预定者手机号
        payment.setCashAmount(Double.parseDouble(reqBean.getInfo("cashAmount"))); // 现金金额
        payment.setPayType(reqBean.getInfo("payType")); // 支付类型，预定/退订
        payment.setPayPlatform(reqBean.getInfo("payPlatform")); // 支付平台
        payment.setOrderTime(DateUtil.parseDateTime(reqBean.getInfo("orderTime"))); // 支付平台订单创建时间
        orderPaymentService.createOrderPayment(payment);
        return Response.responseSuccess();
    }

    /**
     * 步骤五-1：用户完成支付动作
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/completed")
    @ResponseBody
    public JSONObject paymentCompleted(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        orderPaymentService.paymentCompleted(reqBean.getInfo("tradeNo"));
        return Response.responseSuccess();
    }

    /**
     * 步骤五-2：用户取消支付
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/canceled")
    @ResponseBody
    public JSONObject paymentCanceled(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        orderPaymentService.paymentCanceled(reqBean.getInfo("tradeNo"));
        return Response.responseSuccess();
    }

    /**
     * 步骤六：设置支付结果
     * 
     * @param reqBean
     * @return
     */
//    @RequestMapping(value = "/setResult")
//    @ResponseBody
//    public JSONObject paymentSetResult(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
//        orderPaymentService.paymentSetResult(reqBean.getInfo("tradeNo"), "1".equals(reqBean.getInfo("result")),
//                reqBean.getInfo("resultMsg"));
//        return Response.responseSuccess();
//    }
}
